package com.tao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tao.constant.MessageConstant;
import com.tao.constant.StatusConstant;
import com.tao.mapper.SetmealMapper;
import com.tao.mapper.SetmealProductMapper;
import com.tao.pojo.dto.PageSelectSetmealDto;
import com.tao.pojo.dto.SetmealDto;
import com.tao.pojo.entity.Product;
import com.tao.pojo.entity.Setmeal;
import com.tao.pojo.entity.SetmealProduct;
import com.tao.pojo.vo.SetmealByIdVo;
import com.tao.pojo.vo.SetmealVo;
import com.tao.result.PageResult;
import com.tao.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SetmealService1 implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealProductMapper setmealProductMapper;

    @Override
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDto.categoryId")//指定删除一个key
    public void addSetmealService(SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        setmealMapper.addSetmealMapper(setmeal);
        log.info("成功添加套餐:{}", setmeal);

        List<SetmealProduct> setmealProducts = setmealDto.getProducts();
        setmealProducts.forEach(setmealProduct -> {
            setmealProduct.setSetmealId(setmeal.getId());
            setmealProductMapper.addSetmealProduct(setmealProduct);
        });
        //也可以用动态sql批量插入
        log.info("成功添加对应的套餐——产品关系:{}", setmealProducts);
    }

    @Override
    public SetmealByIdVo getById(Integer id) {
        SetmealByIdVo setmealByIdVo = setmealMapper.getById(id);
        setmealByIdVo.setProducts(setmealProductMapper.getBySetmealId(id));
        log.info("所查数据为:{}", setmealByIdVo);
        return setmealByIdVo;
    }

    @Override
    @CacheEvict(cacheNames = "setmealCache", allEntries = true) //删除setmeal下的所有key
    public void deleteSetmeal(List<Integer> ids) {
        log.info("即将删除的队列：{}", ids);
        ids.forEach(id -> {
                    if (setmealMapper.getById(id).getStatus().equals(StatusConstant.ENABLE))
                        throw new RuntimeException(MessageConstant.SETMEAL_ON_SALE);
                }
        );

        ids.forEach(id -> setmealProductMapper.deleteSetmealBySetmealId(id));
        setmealMapper.deleteSetmeal(ids);
        log.info("成功删除setmeal以及setmeal_product对应信息");
    }

    @Override
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public void updateSetmeal(SetmealDto setmealDto) {
        log.info("需要修改的信息为:{}", setmealDto);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        setmealMapper.updateSetmeal(setmeal);
        log.info("成功修改套餐:{}", setmeal);

        setmealProductMapper.deleteSetmealBySetmealId(setmealDto.getId());

        //DTO中的表单无法获取套餐ID，应该手动补充（所有DTO中的来自别的表的List都满足该规律）
        List<SetmealProduct> setmealProducts = setmealDto.getProducts();
        setmealProducts.forEach(setmealProduct -> {
            setmealProduct.setSetmealId(setmealDto.getId());
        });
        setmealDto.getProducts().forEach(setmealProduct -> setmealProductMapper.addSetmealProduct(setmealProduct));
        log.info("成功修改套餐_产品:{}", setmealDto.getProducts());
    }

    @Override
    @CacheEvict(cacheNames = "setmealCache" , allEntries = true)
    public void updateStatus(Integer id, Integer status) {
        log.info("更改套餐:{}的{}状态",id,status);
        if (status.equals(StatusConstant.DISABLE))
            setmealMapper.updateStatus(id, StatusConstant.DISABLE);
        else
            setmealMapper.updateStatus(id, StatusConstant.ENABLE);
        log.info("更改完毕");
    }

    @Override
    public PageResult pageSelect(PageSelectSetmealDto pageSelectSetmealDto) {
        PageHelper.startPage(pageSelectSetmealDto.getPage(),pageSelectSetmealDto.getPageSize());
        Page<SetmealVo> page = setmealMapper.pageSelect(pageSelectSetmealDto);
        page.getResult().forEach(setmealVo -> {
           setmealVo.setProducts(setmealProductMapper.getBySetmealId(setmealVo.getId()));
        });
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Override
    @Cacheable(cacheNames = "setmealCache",key="#categoryId")
    public List<Setmeal> selectByCategoryId(Integer categoryId) {
        List<Setmeal> setmeals=setmealMapper.selectByCategoryId(categoryId);
        log.info("该分类:{}有套餐:{}",categoryId,setmeals);
        return setmeals;
    }

    @Override
    @Cacheable(cacheNames = "setmealProductCache",key = "#id")
    public List<Product> selectSetmealProduct(Integer id) {
        List<Product> products=setmealMapper.selectSetmealProduct(id);
        log.info("该:{}套餐含有菜品:{}",id,products);
        return products;
    }
}
